/*
 * TopoGraphServlet.java
 *
 * Created on September 17, 2007, 4:43 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.web.servlets;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author demory
 */
public class TopoGraphServlet extends FPServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException {
    count_++;
    String sessID = request.getSession().getId();
    Logger qlog = getQueryLogger();
    qlog.log(Level.INFO, "TGS c=" + count_ + " sess=" + sessID + "::doGet entered");

    response.setContentType("image/png");

    //String links = request.getParameter("links");
    int id = new Integer(request.getParameter("id")).intValue();
    int height = 200; // default height if none specified
    if(request.getParameterMap().containsKey("height")) 
      height = new Integer(request.getParameter("height")).intValue();
    
    try {
      
      int[] rgb = server_.getTopoGraphFromID(id, height);
      int width = rgb.length / height;
      BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      bufferedImage.setRGB(0, 0, width, height, rgb, 0, width);
      ImageIO.write(bufferedImage, "png", response.getOutputStream());
      qlog.log(Level.INFO, "TGS c=" + count_ + " sess=" + sessID + "::doGet completed");

    } catch (Exception e) {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<b>Exception caught in TopoGraphServlet:</b><br>");
      StackTraceElement[] st = e.getStackTrace();
      for(int i=0; i<st.length; i++) out.println(st[i].toString()+"<br>");

    }

  }

  @Override
  public void doPost(HttpServletRequest request,
          HttpServletResponse response)
          throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected String getLoggerCode() {
    return "topograph";
  }
}
